package pub.cleangao.codegen.enquiry;

import lombok.Data;
import pub.cleangao.codegen.util.ClassField;

import java.util.List;


@Data
public class TableInfo {

    private String schema;
    private String tableName;
    private String comment;
    private String tableNameHyphen;
    private List<JavaColumnInfo> javaColumnInfos;

    public void setColumnInfos(List<JavaColumnInfo> javaColumnInfos) {
        // 将pk安置在最前
        sort(javaColumnInfos);
        this.javaColumnInfos = javaColumnInfos;

    }

    private void sort(List<JavaColumnInfo> javaColumnInfos) {
        // 将pk移至第一位
        if (javaColumnInfos.get(0).getIsPk()) {
            return;
        }
        for (int i = 0; i < javaColumnInfos.size(); i++) {
            JavaColumnInfo entry = javaColumnInfos.get(i);
            if (entry.getIsPk()) {
                JavaColumnInfo first = javaColumnInfos.get(0);
                javaColumnInfos.set(0, entry);
                javaColumnInfos.set(i, first);
            }
        }
    }

    public String javaTableName() {
        return ClassField.underlineFilter(tableName);
    }

    public boolean getHasDateType() {
        for (JavaColumnInfo columnInfo : javaColumnInfos) {
            if ("Date".equals(columnInfo.getJavaTypeBox())) {
                return true;
            }
        }
        return false;
    }

    public boolean getHasBigDecimalType() {
        for (JavaColumnInfo columnInfo : javaColumnInfos) {
            if ("BigDecimal".equals(columnInfo.getJavaTypeBox())) {
                return true;
            }
        }
        return false;
    }

    public String getPkJavaTypeBox() {
        for (JavaColumnInfo columnInfo : javaColumnInfos) {
            if (columnInfo.getIsPk()) {
                return columnInfo.getJavaTypeBox();
            }
        }
        return null;
    }
}
